/*
 * Copyright (C) 2010 Fred Bojin Liu.
 *
 * Licensed under the GPL, Version 2.0;
 * you may not use this file except in compliance with the License.
 * A copy of the license is included (see the "LICENSE.txt")
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.ucdavis.fredliu.speedtracker;

import android.location.Location;

public class Record {
	private static long firstTime=-1;
	/** The last recorded time, used to calculate current time*/
	private static Long lastTime;
	private static Location lastLoc;
	
	private Location location;
	/** Current time, the time this record is created, in UTC milisec*/
	private long time;
	/** Current speed calculated, in meters/sec */
	private float speed;

	public static long getFirstTime(){
		return Record.firstTime;
	}
	
	private Record(Location loc){
		this.location=loc;
		this.time=System.currentTimeMillis();
	}
	public static Record createRecord(Location loc){
		Record record = new Record(loc);
		if(Record.firstTime==-1){
			Record.firstTime=record.time;
		}
		if(Record.lastTime!=null && Record.lastLoc!=null){
			record.calculateSpeed();
		}
		record.updateLast();
		return record;
	}
	private void calculateSpeed(){
		float distance = this.location.distanceTo(Record.lastLoc);
		this.speed = (float) (distance/((this.time-Record.lastTime)*0.001));
	}
	private void updateLast(){
		Record.lastLoc=this.location;
		Record.lastTime=this.time;
	}
	public double getLon(){
		return this.location.getLongitude();
	}
	public double getLat(){
		return this.location.getLatitude();
	}
	public long getTime(){
		return this.time;
	}
	public float getSpeed(){
		return this.speed;
	}

}
